<template>
  <div class="layout-container">
    <div class="layout-container-form flex space-between">
      <div class="layout-container-form-handle">
        <el-button type="primary" :icon="Plus" @click="handleAdd">{{
          $t("message.common.add")
          }}
        </el-button>
        <el-button type="primary" @click="handleExpand">{{
          expands.length > 0 ? "折叠所有" : "展开所有"
          }}
        </el-button>
      </div>
      <div class="layout-container-form-search">
        <el-input v-model="query.name" :placeholder="$t('message.common.searchTip')"></el-input>
        <el-button type="primary" :icon="Search" class="search-btn" @click="getTableData(true)">{{
          $t("message.common.search")
          }}
        </el-button>
      </div>
    </div>
    <div class="layout-container-table">
      <Table default-expand-all :expand-row-keys="expands" ref="table" v-loading="loading" row-key="menuId"
             :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" :showPage="false" :data="tableData"
             @getTableData="getTableData" @selection-change="handleSelectionChange">
        <el-table-column prop="name" label="菜单名称" align="left" width="200">
          <template #default="scope">
            <i :class="scope.row.icon"></i> {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column prop="menuType" label="菜单类型" align="center">
          <template #default="scope">
            <span class="statusName" v-if="scope.row.menuType === 0">目录</span>
            <span class="statusName" v-if="scope.row.menuType === 1">菜单</span>
            <span class="statusName" v-if="scope.row.menuType === 2">外链</span>
          </template>
        </el-table-column>
        <el-table-column prop="path" label="路由地址" align="center" width="200">
          <template #default="scope">
            {{ scope.row.path.substring(0, 25) }}
          </template>
        </el-table-column>
        <el-table-column prop="weight" label="菜单排序" align="center"/>
        <el-table-column prop="status" label="状态" align="center" width="110">
          <template #default="scope">
            <span class="statusName">{{
                scope.row.status ? "启用" : "禁用"
            }}</span>
            <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949"
                       :loading="scope.row.loading" @click="handleUpdateStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column :label="$t('message.common.handle')" align="center" fixed="right" width="130">
          <template #default="scope">
            <el-button :icon="Edit" @click="handleEdit(scope.row)"></el-button>
            <el-popconfirm :title="$t('message.common.delTip')" @confirm="handleDel([scope.row])">
              <template #reference>
                <el-button :icon="Delete" type="danger"></el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </Table>
      <Layer :layer="layer" @getTableData="getTableData" v-if="layer.show"/>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {ref, reactive} from "vue";
import {getData, del, update} from "@/api/system/menu";
import {LayerInterface} from "@/components/layer/index.vue";
import {ElMessage} from "element-plus";
import Table from "@/components/table/index.vue";
import Layer from "./layer.vue";
import {Plus, Search, Delete, Edit} from "@element-plus/icons";
import menu from "@/locale/modules/en/menu";
// 存储搜索用的数据
const query = reactive({
  name: null,
});
// 弹窗控制器
const layer: LayerInterface = reactive({
  show: false,
  title: "新增",
  showButton: true,
});

const loading = ref(true);
const expands = ref([]);
const tableData = ref([]);
const chooseData = ref([]);
const table = ref();
const handleSelectionChange = (val: []) => {
  chooseData.value = val;
};
// 获取表格数据
// params <init> Boolean ，默认为false，用于判断是否需要初始化分页
const getTableData = (init: Boolean) => {
  loading.value = true;
  getData(query)
      .then((res) => {
        let data = res.data;
        data.forEach((d: any) => {
          d.loading = false;
        });
        tableData.value = data;
      })
      .catch((error) => {
        tableData.value = [];
      })
      .finally(() => {
        loading.value = false;
      });
};
// 删除功能
const handleDel = (data: object[]) => {
  let params = {
    ids: data
        .map((e: any) => {
          return e.menuId;
        })
        .join(","),
  };

  del(params.ids).then((res) => {
    ElMessage({
      type: "success",
      message: "删除成功",
    });
    getTableData(tableData.value.length === 1 ? true : false);
  });
};
// 新增弹窗功能
const handleAdd = () => {
  layer.title = "新增数据";
  layer.show = true;
  delete layer.row;
};
// 编辑弹窗功能
const handleEdit = (row: any) => {
  layer.title = "编辑数据";
  layer.row = row;
  layer.show = true;
};

// 操作折叠展开
const handleExpand = () => {
  let list = tableData.value.map(res => {
    return res.menuId
  })
  if (expands.value.length > 0) {
    expands.value = []
  } else {
    expands.value = list
  }

}
// 状态编辑功能
const handleUpdateStatus = (row: any) => {
  if (!row.menuId) {
    return;
  }
  row.loading = true;
  let params = {
    menuId: row.menuId,
    status: row.status,
  };

  update(params)
      .then((res) => {
        ElMessage({
          type: "success",
          message: "状态变更成功",
        });
      })
      .catch((err) => {
        ElMessage({
          type: "error",
          message: "状态变更失败",
        });
      })
      .finally(() => {
        row.loading = false;
      });
};

getTableData(false);
</script>

<style lang="scss" scoped>
</style>
